package com.ly.video.api.video.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.ly.video.api.user.entity.UserEntity;
import com.ly.video.api.video.entity.VideoEntity;
import com.ly.video.api.video.impl.VideoServiceImpl;
import com.ly.video.common.util.JsonResult;
import com.ly.video.common.util.ResultEnum;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.nio.file.Files;
import java.util.List;
import java.util.Random;

@RequestMapping("video")
@CrossOrigin(origins = "*")
@RestController
public class VideoController {

    private String[] names = {
            "远山只梦里",
            "冬致夏陌",
            "少年听雨歌楼上",
            "黎夕旧梦",
            "森深不见路",
            "悠悠我心",
            "花落伊人两笑欢",
            "ひ陌予倾年",
            "天荒地未老",
            "云卷云舒",
            "伴梦长久",
            "白落薄衫珊阑尽",
            "初秋、繁花入夢",
            "记忆荏苒成泪",
            "时光辗转成歌",
            "微凉离别意",
            "钢琴上的芭蕾",
            "料峭春风吹酒醒",
            "北海以北深海未眠",
            "微笑灿若春花",
            "命里无时莫惦记",
            "夢也渺渺",
            "明我长相忆",
            "烟波流传",
            "如鲸向海",
            "常愿花色新",
            "秦桑低绿枝",
            "落花人独立",
            "恰似一缕微光",
            "山川皆无恙",
            "夜幕篱下浅笙歌",
            "琴声如瑟月微暗",
            "夏天夏星辰" ,
            "秋风秋意凉",
            "莫让浮云遮望眼",
            "梦归所梦",
            "一夕一夏",
            "凉城旧梦空人心",
            "静水踏歌一流年",
            "问女何所思",
            "蝴蝶飞不过沧海",
            "堇墨浮华",
            "一抹微笑",
    };
    @Autowired
    private VideoServiceImpl videoService;

    @RequestMapping(value = "test", method = RequestMethod.GET)
    public JsonResult<Void> test() throws IOException {
        JsonResult<Void> result = new JsonResult<>();

        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI5tA6ugvxfzn4vGBBAFhk";
        String accessKeySecret = "PrfRYy88CiGLyiAU0hHlLMAwjDov9h";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        File dir = new File("/Users/chaiyapeng/Downloads/TikTokDownload-main/Download/douyin/like/舞蹈鉴赏师/");
        int index = 1;
        // 遍历文件夹下所有文件。
        for (File file1 : dir.listFiles()) {
            try {
                for (File file : file1.listFiles()) {
                    if (file.getName().endsWith(".mp4")) {
                        System.out.println("=================="+index+"===================");
                        // 上传文件流。
                        InputStream inputStream = new FileInputStream(file);
                        // 对文件名做MD5加密。
                        String md5 = DigestUtils.md5Hex(file.getName());
                        // 将文件写入Bucket，参数1：Bucket名称  参数2：上传后的文件名  参数3：文件输入流
                        ossClient.putObject("lianyunvideo", md5 + ".mp4", inputStream);
                        String videoURL = "https://lianyunvideo.oss-cn-beijing.aliyuncs.com/" + md5 + ".mp4";

                        InputStream inputStream1 = new FileInputStream("/Users/chaiyapeng/Downloads/python/image/" + index + ".jpeg");
                        // 将文件写入Bucket，参数1：Bucket名称  参数2：上传后的文件名  参数3：文件输入流
                        ossClient.putObject("lianyunvideo", md5 + ".jpeg", inputStream1);
                        String iconURL = "https://lianyunvideo.oss-cn-beijing.aliyuncs.com/" + md5 + ".jpeg";
                        String txt = "";
                        for (File listFile : file1.listFiles()) {
                            if (listFile.getName().endsWith("txt")){
                                txt = Files.readAllLines(listFile.toPath()).get(0);
                            }
                        }
                        VideoEntity entity = new VideoEntity();
                        entity.setUrl(videoURL);
                        entity.setIcon(iconURL);
                        entity.setLikec(new Random().nextInt(1000000));
                        entity.setShare(new Random().nextInt(100000));
                        entity.setContent(txt);
                        entity.setName(names[new Random().nextInt(41) + 1]);
                        videoService.insertVideo(entity);
                        index++;
                    }
                }
            }catch (Exception e) {
            }

        }


        // 关闭OSSClient。
        ossClient.shutdown();

        result.setEnum(ResultEnum.CR_200);
        return result;
    }

    @RequestMapping(value = "/get_video_list", method = RequestMethod.GET)
    public JsonResult<List<VideoEntity>> getVideoListByPage(
            @RequestParam(value = "page", defaultValue = "1") int pageNumber,
            @RequestParam(value = "size", defaultValue = "10") int pageSize
    ) {
        JsonResult<List<VideoEntity>> result = new JsonResult<>();
        List<VideoEntity> videoEntityList = videoService.getVideoListByPage(pageNumber, pageSize);
        result.setEnum(ResultEnum.CR_200);
        result.setData(videoEntityList);
        return result;
    }
}
